<?php namespace Admin\Model;

use Hdphp\Model\Model;

class Product extends Model{
	protected $table = 'shop_product';

	public function addProduct()
	{
		$count = count(current($_POST['attr']));
		for($i=0;$i<$count ;$i++)
		{
			$attr=array();
			foreach($_POST['attr'] as $v)
			{
				$attr[]=$v[$i];
			}
			$_POST['product_attr'][]=implode('-',$attr);
		}
		unset($_POST['attr']);

		foreach($_POST['product_sn'] as $k=>$v)
		{
			$data['goods_id']=$_POST['goods_id'];
			$data['product_sn']=$_POST['product_sn'][$k];
			$data['product_number']=$_POST['product_number'][$k];
			$data['product_attr']=$_POST['product_attr'][$k];
			if(!$this->where('product_attr',$data['product_attr'])->first()){
				$this->add($data);
			}
		}
		return true;
	}

	//获取商品的货品列表
	public function getProduct($goods_id)
	{
		$product= $this->where('goods_id',$goods_id)->get();

		foreach($product as $k=>$v)
		{
			$attr=Db::table('shop_goods_attr')->whereIn('good_attr_id',explode('-',$v['product_attr']))->lists('attr_value');
			$product[$k]['attr']=$attr;

		}
		return $product;
	}

	//修改货品
	public function edit()
	{
		if($this->create())
		{
			return $this->save();
		}
	}
}












